<template>
  <el-form ref="empForm" :rules="rules" label-width="120px" :model="formData">
    <el-form-item prop="username" label="姓名">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item prop="mobile" label="手机">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item prop="timeOfEntry" label="入职时间">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item prop="formOfEmployment" label="聘用形式">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <el-option v-for="ele in empType" :key="ele.id" :label="ele.value" :value="ele.id" />
      </el-select>
    </el-form-item>
    <el-form-item prop="workNumber" label="工号">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item prop="departmentName" label="部门">
      <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="hFocus" />
      <!-- <div v-show="showTree" /> -->
      <el-popover
        v-model="showTree"
        placement="right"
        width="200"
        trigger="manual"
      >
        <el-tree :data="list" :props="{label: 'name'}" @node-click="handleNodeClick" />
      </el-popover>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button>取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { TYPE_MAP } from '@/constant/index'
import obj from '@/constant/employees'
import { getTree } from '@/utils/index'
import { getDepartments } from '@/api/department'
import { addEmployeeAPI } from '@/api/employee'
export default {
  data() {
    return {
      // 控制表单下拉菜单显示隐藏
      showTree: false,
      empType: obj.hireType,
      // [{id:1,value:'非正式'}]
      empType1: Object.keys(TYPE_MAP).map(ele => { return { id: ele, value: TYPE_MAP[ele] } }),
      formData: {
        username: '用户名1', // 用户名
        mobile: '13200000099', // 手机号
        formOfEmployment: '1', // 聘用形式
        workNumber: '12121', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      list: []
    }
  },
  created() {
    console.log(this.empType1)
  },
  methods: {
    resetForm() {
      this.$refs.empForm.resetFields()
    },
    // 点击树形组件节点
    handleNodeClick(data) {
      // console.log(data)
      // 有子级 不让选
      if (data.children.length) return
      // 没子级 渲染进input
      this.formData.departmentName = data.name
      // 隐藏树形组件
      this.showTree = false
    },
    // 部门输入框获取焦点
    hFocus() {
      // 显示树形组件
      this.showTree = true
      // 发请求拿部门信息数据
      this.initDepartments()
    },
    // 发送请求 拿到企业部门列表存data
    async initDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        // 删除数组第一个元素 返回值：被删除的元素
        res.data.depts.shift()
        // 储存列表数据
        this.list = getTree(res.data.depts)
        // 拿出子组件做业务校验需要的数据
        this.newData = res.data.depts.map((ele) => {
          return {
            id: ele.id,
            pid: ele.pid,
            name: ele.name,
            code: ele.code
          }
        })
      } catch (err) {
        console.log(err)
      }
    },
    // 新增
    async doAdd() {
      try {
        await addEmployeeAPI(this.formData)
        this.$message.success('添加成功')
        // console.log('', res)
        // 保存数据
      } catch (err) {
        this.$message.error('添加失败')
        console.log('', err)
      }
    },
    // 表单提交
    hSubmit() {
      this.$refs.empForm.validate(async(valid) => {
        if (!valid) return
        // console.log(12)
        await this.doAdd()
        this.$emit('success')
      })
    }
  }
}
</script>
